package cn.innosmart.aq.customize.addview.ChangeTypeDialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import cn.innosmart.aq.R;
import cn.innosmart.aq.customize.addview.AddProperty.Property;
import cn.innosmart.aq.customize.switchbutton.CheckSwitchButton;

/**
 * 日志模板的栏目属性设置对话框
 * Created by David on 2015/8/4.
 */
public class ChangeTypeDialog extends Dialog {
    private Context mContext;
    private Button bt_positive;
    private Button bt_negative;
    private TextView tv_title;
    private CheckSwitchButton switchButton;
    private RadioGroup radioGroup;
    private RadioButton radioText;
    private RadioButton radioNum;
    private RadioButton radioDate;
    private CharSequence mPositiveButtonText;
    private View.OnClickListener mPositiveButtonListener;
    private CharSequence mNegativeButtonText;
    private View.OnClickListener mNegativeButtonListener;
    private View.OnClickListener mBtnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_positive:
                    mPositiveButtonListener.onClick(v);
                    break;
                case R.id.bt_negative:
                    mNegativeButtonListener.onClick(v);
                    break;
            }
        }
    };
    private String type;
    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_normal:
                    type = "TEXT";
                    break;
                case R.id.rb_important:
                    type = "NUM";
                    break;
                case R.id.rb_high_important:
                    type = "DATE";
                    break;
            }
        }
    };

    private Property property;
    private OnChangeListener onChangeListener;

    public ChangeTypeDialog(Context context, Property property) {
        super(context, R.style.dialog);
        init(context, property);
    }

    public ChangeTypeDialog(Context context, int theme, Property property) {
        super(context, theme);
        init(context, property);
    }

    public ChangeTypeDialog(Context context, boolean cancelable, OnCancelListener cancelListener, Property property) {
        super(context, cancelable, cancelListener);
        init(context, property);
    }

    private void init(Context context, Property property) {
        this.mContext = context;
        this.property = property;
        View contentView = View.inflate(getContext(), R.layout.dialog_change_type, null);
        setContentView(contentView);
        bt_positive = (Button) findViewById(R.id.bt_positive);
        bt_negative = (Button) findViewById(R.id.bt_negative);
        bt_negative.setOnClickListener(mBtnOnClickListener);
        bt_positive.setOnClickListener(mBtnOnClickListener);
        tv_title = (TextView) findViewById(R.id.tv_title);
        switchButton = (CheckSwitchButton) findViewById(R.id.switch_buton);
        radioText = (RadioButton) findViewById(R.id.rb_normal);
        radioNum = (RadioButton) findViewById(R.id.rb_important);
        radioDate = (RadioButton) findViewById(R.id.rb_high_important);
        radioGroup = (RadioGroup) findViewById(R.id.rg_group);
        if (property.isRequired()) {
            switchButton.setChecked(true);
        } else {
            switchButton.setChecked(false);
        }
        type = property.getType();
        if (property.getType().equals("TEXT")) {
            radioText.setChecked(true);
        } else if (property.getType().equals("NUM")) {
            radioNum.setChecked(true);
        } else if (property.getType().equals("DATE")) {
            radioDate.setChecked(true);
        } else {

        }
        radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
//        //getWindow().setWindowAnimations(R.anim.alpha_in);
        /**
         * 强制设置点击外部不取消对话框
         */
        setCanceledOnTouchOutside(false);
    }

    public void setTitle(CharSequence title) {
        tv_title.setText(title);
    }

    public void setTitle(int titleId) {
        tv_title.setText(mContext.getText(titleId));
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
    }

    public void setPositiveButton(int textId, View.OnClickListener listener) {
        mPositiveButtonText = mContext.getText(textId);
        bt_positive.setVisibility(View.VISIBLE);
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setPositiveButton(CharSequence text, View.OnClickListener listener) {
        mPositiveButtonText = text;
        bt_positive.setVisibility(View.VISIBLE);
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setNegativeButton(int textId, View.OnClickListener listener) {
        mNegativeButtonText = mContext.getText(textId);
        bt_negative.setVisibility(View.VISIBLE);
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    public void setNegativeButton(CharSequence text, View.OnClickListener listener) {
        mNegativeButtonText = text;
        bt_negative.setVisibility(View.VISIBLE);
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    @Override
    public void show() {
        super.show();
    }

    public void setOnChangeListener(OnChangeListener OnChangeListener) {
        this.onChangeListener = OnChangeListener;
    }

    public void checkChange() {
        if (onChangeListener != null) {
            onChangeListener.onChange(type, switchButton.isChecked());
        }
    }

    public interface OnChangeListener {
        void onChange(String type, boolean isRequired);
    }
}
